#include <iostream>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <cstring>
#include <unistd.h>

int main() {
    key_t key = ftok("shmfile", 65);

    // 创建共享内存
    int shmid = shmget(key, 1024, 0666 | IPC_CREAT);
    
    // 将共享内存附加到当前进程
    char *str = (char*) shmat(shmid, nullptr, 0);

    // 写入数据
    std::string message = "i am process A";
    strncpy(str, message.c_str(), message.size() + 1);
    
    std::cout << "Process A wrote to shared memory: " << message << std::endl;

    // 分离共享内存
    shmdt(str);
    return 0;
}
